﻿namespace DeltaGroup.WheelOfJeopardy.UI.ViewModel
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;

    using DeltaGroup.WheelOfJeopardy.Model;

    public class SetupModel : BaseViewModel, IDataErrorInfo
    {
        #region Fields

        private DifficultyLevel _difficulty;
        private int _numberOfContestants;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SetupModel"/> class.
        /// </summary>
        public SetupModel()
        {
            _numberOfContestants = 2;
            _difficulty = DifficultyLevel.Medium;
            Contestants = new ObservableCollection<NewContestantModel>()
                              {
                                  new NewContestantModel(1),
                                  new NewContestantModel(2)
                              };
        }

        #endregion Constructors

        #region Properties

        /// <summary>
        /// Gets or sets the challenge files.
        /// </summary>
        /// <value>The challenge files.</value>
        public List<string> ChallengeFiles
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets the contestants.
        /// </summary>
        /// <value>The contestants.</value>
        public ObservableCollection<NewContestantModel> Contestants
        {
            get; set;
        }

        /// <summary>
        /// Gets or sets the difficulty.
        /// </summary>
        /// <value>The difficulty.</value>
        public DifficultyLevel Difficulty
        {
            get { return _difficulty; }
            set
            {
                _difficulty = value;
                FirePropertyChanged("Difficulty");
            }
        }

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <value></value>
        /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
        public string Error
        {
            get { return null; }
        }

        /// <summary>
        /// Gets or sets the number of contestants.
        /// </summary>
        /// <value>The number of contestants.</value>
        public int NumberOfContestants
        {
            get { return _numberOfContestants; }
            set
            {
                var newNum = value;

                if (newNum > _numberOfContestants)
                {
                    for (var i = 0; i < newNum - _numberOfContestants; i++)
                    {
                        Contestants.Add(new NewContestantModel(_numberOfContestants + 1 + i));
                    }
                }
                else if (newNum < _numberOfContestants)
                {
                    for (var i = 0; i < _numberOfContestants - newNum; i++)
                    {
                        Contestants.RemoveAt(Contestants.Count - 1);
                    }
                }
                _numberOfContestants = value;

                FirePropertyChanged("NumberOfContestants");
            }
        }

        /// <summary>
        /// Gets or sets the selected challenge file.
        /// </summary>
        /// <value>The selected challenge file.</value>
        public string SelectedChallengeFile
        {
            get; set;
        }

        #endregion Properties

        #region Indexers

        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified column name.
        /// </summary>
        /// <value></value>
        public string this[string columnName]
        {
            get
            {
                if (columnName == "SelectedChallengeFile")
                    if (string.IsNullOrWhiteSpace(SelectedChallengeFile))
                        return "Please select a file";
                return null;
            }
        }

        #endregion Indexers
    }
}